package com.qububu.play.dineTogether.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.play.dineTogether.dto.StaffInfoDto;
import com.qububu.play.dineTogether.service.DineStaffService;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

@Controller
@RequestMapping("/play/dine")
public class DineStaffControl {

	@Resource
	private DineStaffService dineStaffService;

	/**
	 * 进入添加任务的界面
	 * */
	@RequestMapping("/admin/infoAddStaff")
	public String infoAddStaff(HttpServletRequest req) {
		
		String actionId = assertNotEmpty(req.getParameter("actionId"));
		
		req.setAttribute("actionId", actionId);
		return "adminDineStaff";
	}
	
	/**
	 * @功能 添加工作
	 * @备注 操作表单staff 
	 */
	@RequestMapping("/admin/ajaxAddStaff")
	@ResponseBody
	public Map<String, Object> addStaff(HttpServletRequest request, HttpServletResponse response){
		
		Map<String, Object> staffMap = new HashMap<String, Object>();

		String staffId = SystemUtil.getUUIDString();
		String staffName = SystemUtil.convertSpecialMark(assertNotEmpty(request.getParameter("staffName")));
		String explain = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(request.getParameter("explain")));
		String ActionId = assertNotEmpty(request.getParameter("actionId"));
	
		StaffInfoDto staffInfo = createStaffEntity(staffId, staffName, explain, ActionId);
		if (dineStaffService.doAddStaffInfo(staffInfo)) {
			staffMap.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			staffMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			staffMap.put("message", "缓存处理失败,请稍后重试");
		}
		
		
		return staffMap;
	}

	/**
	 * 删除工作
	 * */
	
	/**
	 * 修改工作
	 * */
	@RequestMapping("/admin/modifyStaff")
	@ResponseBody
	public Map<String, Object> modifyStaff(HttpServletRequest req) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		String actionId = SystemUtil.getUUIDString();
		String staffId = assertNotEmpty(req.getParameter("staffId"));
		String staffName = SystemUtil.convertSpecialMark(assertNotEmpty(req.getParameter("staffName")));
		String explain = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(req.getParameter("explain")));
		
		StaffInfoDto staffInfo = createStaffEntity(staffId, staffName, explain, actionId);
		
		if (dineStaffService.doModifyStaff(staffInfo)) {
			resultMap.put("result", SystemConstants.AJAX_SUCCESS);	
		} else {
			resultMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			resultMap.put("message", "缓存处理失败,请稍后重试");
		}
		
		return resultMap;
	}
	
	/**
	 * 查询工作
	 * */
	@RequestMapping("/getStaffs")
	@ResponseBody
	public Map<String, Object> getStaffs(HttpServletRequest req) {
		Map<String, Object> staffListMap = new HashMap<String, Object>();
		
		String actionId = assertNotEmpty(req.getParameter("actionId"));
		
		List<StaffInfoDto> staffList = dineStaffService.getStaffListByActionId(actionId);
		
		staffListMap.put("result", SystemConstants.AJAX_SUCCESS);
		staffListMap.put("staffs", staffList);
		
		return staffListMap;
	}
	
	/**
	 * 删除工作
	 * */
	@RequestMapping("/admin/deleteStaff")
	@ResponseBody
	public Map<String, Object> deleteStaff(HttpServletRequest req) {
		
		String staffId = assertNotEmpty(req.getParameter("staffId"));

		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (dineStaffService.doDeleteStaff(staffId)) {
			resultMap.put("result", SystemConstants.AJAX_SUCCESS);	
		} else {
			resultMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			resultMap.put("message", "缓存处理失败, 请稍后重试");
		}
		
		return resultMap;
	}
	
	/**
	 * @功能 创建staffEntity类 
	 * @param staffId
	 * @param staffName
	 * @param explain
	 * @return
	 */
	private StaffInfoDto createStaffEntity(String staffId, String staffName,
			String explain, String actionId) {
		StaffInfoDto staffInfoDto = new StaffInfoDto(staffId, actionId);
		
		staffInfoDto.setExplain(explain);
		staffInfoDto.setStaffName(staffName);
		
		return staffInfoDto;
	}
	
}
